package com.heima.service.impl.Sku;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.controller.exception.BusinessException;
import com.heima.mapper.Sku.SkuMapper;
import com.heima.pojo.Sku;
import com.heima.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;


@Slf4j
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {


    @Autowired
    HttpServletRequest request;

    //添加
    @Override
    public boolean add(Sku sku) {
        String myfile = (String) request.getSession().getAttribute("myfile");
        //判断 sku_name 是否存在  根据 名字查询
        LambdaQueryWrapper<Sku> qw = new LambdaQueryWrapper<>();
        qw.eq(sku.getSkuName() != null, Sku::getSkuName, sku.getSkuName());

        Sku Mysku = this.getOne(qw);
        //不等于null
        if (Mysku != null) {
            throw new BusinessException("名字已经存在");
        }
        sku.setIsDiscount(0);
        sku.setSkuImage(myfile);
        log.info("图片信息是{}",myfile);
        System.out.println("myfile = " + myfile);
        //进行保存
        this.save(sku);
        return true;
    }
}
